import {Component, OnInit} from '@angular/core';
import {ListMemMemberComponent} from '../list-mem-member.component';
import {isNullOrUndefined} from 'util';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {MemMemberCondition} from '../../mem-member.type';
import {Services} from '../../../../../frame/module/services';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';

@Component({
    selector: 'app-table-list-mem-member',
    templateUrl: './table-list-mem-member.component.html',
    styleUrls: ['./table-list-mem-member.component.css']
})
export class TableListMemMemberComponent implements OnInit {

    zero3Enum = Zero3Enum;

    data;

    head = {
        tr: {
            selectedAll: {
                icon: fa.faCheckCircle,
                click: (event) => {
                    if (this.body.tr.selectedUuid.size === 0) {
                        this.data.page.content.forEach(item => {
                            this.body.tr.selectedUuid.set(item.uuid, item.uuid);
                        });
                        this.head.tr.selectedAll.icon = fa.faCheck;
                    } else {
                        this.body.tr.selectedUuid.clear();
                        this.head.tr.selectedAll.icon = fa.faCheckCircle;
                    }
                }
            }
        },
        orderOrderCount: 0,
        serMemo: {
            icon: null,
            name: '备注',
            fieldName: 'serMemo',
            orderType: '',
            orderOrder: null,
            sort: (event) => {
                this.head.changeSort(this.head.serMemo.fieldName);
            }
        },
        createTime: {
            icon: null,
            name: '创建时间',
            fieldName: 'createTime',
            orderType: '',
            orderOrder: null,
            sort: (event) => {
                this.head.changeSort(this.head.createTime.fieldName);
            }
        },
        serAccountNo: {
            icon: null,
            name: '账号',
            fieldName: 'serAccountNo',
            orderType: '',
            orderOrder: null,
            sort: (event) => {
                this.head.changeSort(this.head.serAccountNo.fieldName);
            }
        },
        serPhone: {
            icon: null,
            name: '电话',
            fieldName: 'serPhone',
            orderType: '',
            orderOrder: null,
            sort: (event) => {
                this.head.changeSort(this.head.serPhone.fieldName);
            }
        },
        serScore: {
            icon: null,
            name: '积分',
            fieldName: 'serScore',
            orderType: '',
            orderOrder: null,
            sort: (event) => {
                this.head.changeSort(this.head.serScore.fieldName);
            }
        },
        serGoldCoin: {
            icon: null,
            name: '金币',
            fieldName: 'serGoldCoin',
            orderType: '',
            orderOrder: null,
            sort: (event) => {
                this.head.changeSort(this.head.serGoldCoin.fieldName);
            }
        },
        changeSort: (fieldName) => {
            switch (this.head[fieldName].orderType) {
                case '':
                    this.head[fieldName].orderType = 'asc';
                    this.head[fieldName].icon = fa.faSortUp;
                    this.head.orderOrderCount += 1;
                    this.head[fieldName].orderOrder = this.head.orderOrderCount;
                    break;
                case 'asc':
                    this.head[fieldName].orderType = 'desc';
                    this.head[fieldName].icon = fa.faSortDown;
                    break;
                case 'desc':
                    this.head[fieldName].orderType = '';
                    this.head[fieldName].icon = null;
                    this.head.orderOrderCount -= 1;
                    Reflect.ownKeys(this.head).forEach(item => {
                        const fieldObj = Reflect.get(this.head, item);
                        if (!isNullOrUndefined(fieldObj.orderOrder)) {
                            fieldObj.orderOrder -= 1;
                        }
                    });
                    this.head[fieldName].orderOrder = null;
                    break;
            }
            this.listMemMember.func.list.click(null);
        },
        orderBy: () => {
            let orderBy = '';
            const orderBys = [];
            Reflect.ownKeys(this.head).forEach(item => {
                const fieldObj = Reflect.get(this.head, item);
                if (!isNullOrUndefined(fieldObj.orderOrder)) {
                    orderBys.push(fieldObj);
                }
            });
            orderBys.sort((a, b) => {
                return a.orderOrder - b.orderOrder;
            });
            orderBys.forEach(item => {
                if (item.orderType.length > 0) {
                    orderBy += `,${item.fieldName}-${item.orderType}`;
                }
            });
            return orderBy.length === 0 ? 'createTime-desc,serNum-asc' : orderBy.substring(1);
        }
    };

    body = {
        tr: {
            selectedUuid: new Map(),
            isSelected: (uuid) => {
                const value = this.body.tr.selectedUuid.get(uuid);
                return !isNullOrUndefined(value);
            },
            click: (event, uuid) => {
                const value = this.body.tr.selectedUuid.get(uuid);
                if (isNullOrUndefined(value)) {
                    this.body.tr.selectedUuid.set(uuid, '0');
                } else {
                    this.body.tr.selectedUuid.delete(uuid);
                }
            },
            dbclick: (event, uuid) => {
                console.log('dbclick');
                const condition = new MemMemberCondition();
                condition.uuidList = [uuid];
                Services.memMember.modify(condition, this.listMemMember.module);
                console.log('fun modify');
            }
        }
    };

    list = () => {
        this.listMemMember.func.list.click(null);
    };

    constructor(public listMemMember: ListMemMemberComponent) {
        this.listMemMember.table = this;
    }

    ngOnInit() {
    }

}
